<?php
/*
 $Id$ preview.php
 ECG, http://www.gtf.16mb.com
 Copyright (c) 2012 Eugene.K
 
 This program is shareware software; all right reserved 
*/


/*
 * The admin side of the downloads plagin
*/

  $Qp = $osC_Database->query('select p.products_id, p.products_quantity, p.products_price, p.products_model, p.products_weight, p.products_weight_class, p.products_date_added, p.products_last_modified, p.products_status, p.products_tax_class_id, p.manufacturers_id, i.image from :table_products p left join :table_products_images i on (p.products_id = i.products_id and default_flag = :default_flag) where p.products_id = :products_id');
  $Qp->bindTable(':table_products', TABLE_PRODUCTS);
  $Qp->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
  $Qp->bindInt(':products_id', $_GET[$osC_Template->getModule()]);
  $Qp->bindInt(':default_flag', 1);
  $Qp->execute();

  $Qpd = $osC_Database->query('select products_name, products_description, products_url, language_id from :table_products_description where products_id = :products_id');
  $Qpd->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
  $Qpd->bindInt(':products_id', $_GET[$osC_Template->getModule()]);
  $Qpd->execute();

  //*** START ***********************************************************************************
  $DwnldString  = MODULE_SERVICES_INSTALLED;
  $DwnldArray = explode(";", $DwnldString);
  $DwnldSelector=in_array('downloads', $DwnldArray);
  $FileLicenseDomaineName='online-gtf.16mb.com';
  
  if ($DwnldSelector)
  {
  	if(!defined('SERVICE_DOWNLOADS_LICENSE_VALUE') || SERVICE_DOWNLOADS_LICENSE_VALUE == '' || !defined('SERVICE_DOWNLOADS_LICENSE_ID') || SERVICE_DOWNLOADS_LICENSE_ID == '')
  	{
  		$DwnldSelector=false;
  	}else
  	{
  		if (!is_numeric(SERVICE_DOWNLOADS_LICENSE_ID) || strlen(SERVICE_DOWNLOADS_LICENSE_VALUE)!=19)
  			$DwnldSelector=false;
  		else
  		{
  			if ($FileLicenseDomaineName!=$_SERVER['HTTP_HOST'])
  			{
  				$DwnldSelector=false;
  			}
  		}
  	}
  }
  
  if ($DwnldSelector)
  {
	$Qpf = $osC_Database->query('select id, file_name, file_user_name, file_size, allow_download, deletedflag, date_added from :table_products_files where products_id = :products_id');
	$Qpf->bindTable(':table_products_files', TABLE_PRODUCTS_FILES);
	$Qpf->bindInt(':products_id', $_GET[$osC_Template->getModule()]);
	$Qpf->execute();
  
	if ($Qpf->numberOfRows() > 0)
	{
		$pf_extra = array();
		$fileDescription=array();
  		while ( $Qpf->next())
  		{
  			$pf_extra[$Qpf->valueInt('id')]['products_file']=$Qpf->valueProtected('file_name');
  			$pf_extra[$Qpf->valueInt('id')]['products_original_file']=$Qpf->valueProtected('file_user_name');
  			$pf_extra[$Qpf->valueInt('id')]['products_file_size']=$Qpf->valueInt('file_size');
  			$pf_extra[$Qpf->valueInt('id')]['allow_download']=$Qpf->valueInt('allow_download');
			$pf_extra[$Qpf->valueInt('id')]['deletedflag']=$Qpf->valueInt('deletedflag');
  			$pf_extra[$Qpf->valueInt('id')]['products_date']=date('d.m.Y', strtotime($Qpf->value('date_added')));

  			$QpfDesc = $osC_Database->query('select file_id, language_id, file_description, file_note from :table_products_files_description where file_id = :file_id');
  			$QpfDesc->bindTable(':table_products_files_description', TABLE_PRODUCTS_FILE_DESCRIPTION);
  			$QpfDesc->bindInt(':file_id', $Qpf->valueInt('id'));
  			$QpfDesc->execute();
  			if ($QpfDesc->numberOfRows() > 0)
  			{
  				while ( $QpfDesc->next())
  				{
  					$pf_extra[$Qpf->valueInt('id')]['file_description'][$QpfDesc->valueInt('language_id')]=$QpfDesc->value('file_description');
					$pf_extra[$Qpf->valueInt('id')]['file_note'][$QpfDesc->valueInt('language_id')]=$QpfDesc->value('file_note');
  				}
  			}
  		}
  	}
  }
  //*** END ***********************************************************************************
  
  $pd_extra = array();
  while ( $Qpd->next() ) {
    $pd_extra['products_name'][$Qpd->valueInt('language_id')] = $Qpd->valueProtected('products_name');
    $pd_extra['products_description'][$Qpd->valueInt('language_id')] = $Qpd->value('products_description');
    $pd_extra['products_url'][$Qpd->valueInt('language_id')] = $Qpd->valueProtected('products_url');
  }

  $osC_ObjectInfo = new osC_ObjectInfo(array_merge($Qp->toArray(), $pd_extra));

  $products_name = $osC_ObjectInfo->get('products_name');
  $products_description = $osC_ObjectInfo->get('products_description');
  $products_url = $osC_ObjectInfo->get('products_url');
?>



<?php
  if ( $osC_MessageStack->exists($osC_Template->getModule()) ) {
    echo $osC_MessageStack->get($osC_Template->getModule());
  }
?>

<div style="background-color: #DEEDF7;">

<?php
  foreach ( $osC_Language->getAll() as $l ) {
    echo '<span id="lang_' . $l['code'] . '"' . (($l['code'] == $osC_Language->getCode()) ? ' class="highlight"' : '') . '><a href="javascript:toggleDivBlocks(\'pName_\', \'pName_' . $l['code'] . '\'); toggleClass(\'lang_\', \'lang_' . $l['code'] . '\', \'highlight\', \'span\');">' . $osC_Language->showImage($l['code']) . '</a></span>&nbsp;&nbsp;';
  }
?>

</div>

<?php
  foreach ( $osC_Language->getAll() as $l ) {
?>
<?php /*
<div id="pName_<?php echo $l['code']; ?>" <?php echo (($l['code'] != $osC_Language->getCode()) ? ' style="display: none;"' : ''); ?>>
  <table border="0" width="100%" cellspacing="0" cellpadding="2">
    <tr>
      <td><h1><?php echo osc_output_string_protected($products_name[$l['id']]) . (!osc_empty($osC_ObjectInfo->get('products_model')) ? '<br /><span>' . $osC_ObjectInfo->getProtected('products_model') . '</span>': ''); ?></h1></td>
	  <td align="right"><h1><?php echo $osC_Currencies->format($osC_ObjectInfo->get('products_price')); ?></h1></td>
    </tr>
  </table>
*/?>

<?php // ********START NEW VIEW******* ?>
<div id="pName_<?php echo $l['code']; ?>" <?php echo (($l['code'] != $osC_Language->getCode()) ? ' style="display: none;"' : ''); ?>>
  <table border="0" width="100%" cellspacing="0" cellpadding="1">
    <tr>
      <td align="center"><h1><?php echo $products_name[$l['id']]; ?></h1></td>
    </tr>
  </table>
<?php // ********END NEW VIEW******* ?>

  <p><?php echo $osC_Image->show($osC_ObjectInfo->get('image'), $products_name[$l['id']], 'align="left" hspace="5" vspace="5"', 'product_info') . $products_description[$l['id']]; ?></p>

<?php 
	//*** START ***********************************************************************************
	if ($DwnldSelector)
	{
		if(isset($pf_extra) && !empty($pf_extra))
		{
			print '<fieldset>
			<legend><b>'.$osC_Language->get('file_product_information').':</legend></b><br>';

			foreach ($pf_extra as $key=>$value)
			{
				$formatedFileSize=round($pf_extra[$key]['products_file_size'], 2);
				$FileSizeDescription='bytes';
				if ($pf_extra[$key]['products_file_size']>=1024)
				{
					$formatedFileSize=round($pf_extra[$key]['products_file_size']/1024, 2);
					$FileSizeDescription='kB';
				}
				if ($pf_extra[$key]['products_file_size']>=1048576)
				{
					$formatedFileSize=round($pf_extra[$key]['products_file_size']/1024/1024, 2);
					$FileSizeDescription='mB';
				}
			
				if ($pf_extra[$key]['allow_download']==1)
				{
					$DownloadPermissionText=$osC_Language->get('file_download_permission_text_on');
				}else
				{
					$DownloadPermissionText=$osC_Language->get('file_download_permission_text_off');
				}
			
				print '<b>'.$pf_extra[$key]['file_description'][$l['id']].':</b><br>
				<li>'.$osC_Language->get('file_product_name').': '.$pf_extra[$key]['products_file'].'</li>
				<li>'.$osC_Language->get('file_product_original_name').': '.$pf_extra[$key]['products_original_file'].'</li>
				<li>'.$osC_Language->get('file_product_size').': '.$formatedFileSize.' '.$FileSizeDescription.'</li>
				<li>'.$osC_Language->get('file_product_loaded').': '.$pf_extra[$key]['deletedflag'].' '.$osC_Language->get('file_product_loaded_count').'</li>
				<li>'.$osC_Language->get('file_added_date').': '.$pf_extra[$key]['products_date'].'</li>
				<li>'.$osC_Language->get('file_download_permission_title').': <b>'.$DownloadPermissionText.'</b></li>
				<li>'.$osC_Language->get('file_note').': <br>'.$pf_extra[$key]['file_note'][$l['id']].'</li>
				<hr>';
			}
			print'</fieldset>';
		}
	}else
		print '<font color="red"><b>'.$osC_Language->get('file_product_configuration_need').'</b></font>';
	//*** END ***********************************************************************************
?>
<?php
    if ( !empty($products_url[$l['id']]) ) {
      echo '<p>' . sprintf($osC_Language->get('more_product_information'), osc_output_string_protected($products_url[$l['id']])) . '</p>';
    }
?>

<?php
// HPDL
//    if ($osC_ObjectInfo->get('products_date_available') > date('Y-m-d')) {
//      echo '<p align="center">' . sprintf($osC_Language->get('product_date_available'), osC_DateTime::getLong($osC_ObjectInfo->get('products_date_available'))) . '</p>';
//    } else {
      echo '<p align="left"><br /><b>' . sprintf($osC_Language->get('product_date_added'), osC_DateTime::getLong($osC_ObjectInfo->get('products_date_added'))) . '</b></p>';
//    }
?>

</div>

<?php
  }
?>

<p align="left"><?php echo '<input type="button" value="' . $osC_Language->get('button_back') . '" onclick="document.location.href=\'' . osc_href_link_admin(FILENAME_DEFAULT, $osC_Template->getModule() . '&cID=' . $_GET['cID']) . '\';" class="operationButton" />'; ?></p>
